//
//  AppDelegate.m
//  ProjectB_
//
//  Created by dllo on 16/3/21.
//  Copyright © 2016年 项目B. All rights reserved.
//

#import "AppDelegate.h"

#import "BaseViewController.h"  //C

#import "SDP_ViewControllerForCollectionTopRanking.h"

#import "SDP_ViewControllerForFindOutMore.h"  //V

#import "CWG_ViewControllerForDengLu.h"

#import <AVOSCloud/AVOSCloud.h>

#import <AVOSCloudSNS.h>

@interface AppDelegate ()

@property (strong, nonatomic) UIButton *buttonForEverydaySelected;   //button的标记值是:100
@property (strong, nonatomic) UIButton *buttonForHot;       //button的标记值是:200
@property (strong, nonatomic) UIButton *buttonForFindOutMore;    //button的标记值是:300
@property (nonatomic, strong) UIButton *selectedBtn;
@property (nonatomic, strong) UIView *myView;
@property (nonatomic, strong) UITabBarController *tb;

@end

#define HEIGHT self.window.bounds.size.height
#define WIDTH  self.window.bounds.size.width
@implementation AppDelegate

- (void)dealloc {
    
    // 移除通知中心
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    return [AVOSCloudSNS handleOpenURL:url];
}

// When Build with IOS 9 SDK
// For application on system below ios 9
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [AVOSCloudSNS handleOpenURL:url];
}
// For application on system equals or larger ios 9
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
    return [AVOSCloudSNS handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //如果使用美国站点，请加上这行代码 [AVOSCloud useAVCloudUS];
    [AVOSCloud setApplicationId:@"RiRrOGh80wPmiRr4mhsd9Pyb-gzGzoHsz"
                      clientKey:@"TgkeUIUlpQMI6PfGEYkGMRn1"];
    
    AVObject *testObject = [AVObject objectWithClassName:@"TestObject"];
    [testObject setObject:@"bar" forKey:@"foo"];
    [testObject save];
    
    // SQLite
    [[DataBaseHandler shareDataBaseHandler] open];
    [[DataBaseHandler shareDataBaseHandler] createTable];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];
    self.window.backgroundColor = [UIColor whiteColor];
    
    self.tb = [[UITabBarController alloc] init];
    self.tb.tabBar.hidden = YES;
    
    [self createItems];
    
    self.window.rootViewController = self.tb;
    
    [self createCustomTabBarView];
  
   // __unsafe_unretained __block id weakSelf = self;
    [[NSNotificationCenter defaultCenter] addObserverForName:@"隐藏" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
       
        [UIView animateWithDuration:0.15 animations:^{
            
            self.myView.alpha = 0;
        }];
      
    }];
    [[NSNotificationCenter defaultCenter] addObserverForName:@"显示" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
       
        [UIView animateWithDuration:0.3 animations:^{
           
            self.myView.alpha = 1;
        }];
        
    }];
    
    
    // 启动图
    [YYClipImageTool addToCurrentView:self.window clipImage:[UIImage imageNamed:@"LaunchImage"] backgroundImage:nil];
    
    return YES;
}

#pragma mark - 创建多个Items
- (void)createItems {
    
    // Item1(每日精选)
    BaseViewController *vc = [[BaseViewController alloc] init];
    vc.url = @"http://baobab.wandoujia.com/api/v2/feed?_s=e48de6c45c4b5696bf50bb93efb030f1&date=1458748800000&f=iphone&net=wifi&num=7&u=693b683a5db7a43954ed61f0fad8d5f216b94455&v=1.12.1&vc=403";
    UINavigationController *navi1 = [[UINavigationController alloc] initWithRootViewController:vc];
    
    
    // Item2(发现更多)
    SDP_ViewControllerForFindOutMore *sdp_VC = [[SDP_ViewControllerForFindOutMore alloc] init];
    UINavigationController *navi2 = [[UINavigationController alloc] initWithRootViewController:sdp_VC];
    
    // Item3(热门排行)
    SDP_ViewControllerForCollectionTopRanking *sdp_VCForTopRanking = [[SDP_ViewControllerForCollectionTopRanking alloc]init];
    UINavigationController *navi3 = [[UINavigationController alloc] initWithRootViewController:sdp_VCForTopRanking];
    
    self.tb.viewControllers = @[navi1, navi2, navi3];
}

#pragma mark - 创建自定义TabBarView
- (void)createCustomTabBarView {
    
    self.myView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT - 49, WIDTH, 49)];
    self.myView.clipsToBounds = YES;
    self.myView.contentMode = UIViewContentModeBottom;
    [self.window addSubview:self.myView];
    self.myView.backgroundColor = RGBA(241, 239, 217, 1);
    
    UIView *viewForLineLeft = [[UIView alloc] initWithFrame:CGRectMake(WIDTH * 1 / 3 - 2 / 2, 10, 2, self.myView.frame.size.height - 20)];
    [self.myView addSubview:viewForLineLeft];
    viewForLineLeft.backgroundColor = [UIColor lightGrayColor];
    
    UIView *viewForLineRight = [[UIView alloc] initWithFrame:CGRectMake(WIDTH * 2 / 3 - 2 / 2, 10, 2, self.myView.frame.size.height - 20)];
    [self.myView addSubview:viewForLineRight];
    viewForLineRight.backgroundColor = [UIColor lightGrayColor];
    
    // 创建button: 发现更多
    self.buttonForFindOutMore = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonForFindOutMore.tag = 1;
    [self.buttonForFindOutMore setTitle:@"发现更多" forState:UIControlStateNormal];
    [self.buttonForFindOutMore.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [self.buttonForFindOutMore setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.buttonForFindOutMore setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    self.buttonForFindOutMore.frame = CGRectMake(viewForLineLeft.frame.origin.x + viewForLineLeft.frame.size.width + WIDTH / 18.75, 15, WIDTH * 2 / 3 - WIDTH * 1 / 3 - 2 - WIDTH / 18.75 * 2, 49 - 30);
    [self.buttonForFindOutMore addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:self.buttonForFindOutMore];
    
    // 创建button: 每日精选
    self.buttonForEverydaySelected = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonForEverydaySelected.tag = 0;
    [self.buttonForEverydaySelected setTitle:@"每日精选" forState:UIControlStateNormal];
    [self.buttonForEverydaySelected.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [self.buttonForEverydaySelected setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.buttonForEverydaySelected setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    self.buttonForEverydaySelected.frame = CGRectMake(WIDTH / 18.75, 15, self.buttonForFindOutMore.frame.size.width, 49 - 30);
    [self.buttonForEverydaySelected addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:self.buttonForEverydaySelected];
    
    // 创建button: 热门排行
    self.buttonForHot = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonForHot.tag = 2;
    [self.buttonForHot setTitle:@"热门排行" forState:UIControlStateNormal];
    [self.buttonForHot.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [self.buttonForHot setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.buttonForHot setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    self.buttonForHot.frame = CGRectMake(WIDTH * 2 / 3 + viewForLineRight.frame.size.width / 2 + WIDTH / 18.75, 15, self.buttonForEverydaySelected.frame.size.width, 49 - 30);
    [self.buttonForHot addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:self.buttonForHot];
    
    self.buttonForEverydaySelected.selected = YES;
    self.buttonForEverydaySelected.tintColor = [UIColor clearColor];
    self.selectedBtn = self.buttonForEverydaySelected;
    
    
    
    
}

- (void)clickButton:(UIButton *) button {
    
    //1.先将之前选中的按钮设置为未选中
    self.selectedBtn.selected = NO;
    //2.再将当前按钮设置为选中
    button.selected = YES;
    
    if (button.selected) {
        button.tintColor = [UIColor clearColor];
    }
    
    //3.最后把当前按钮赋值为之前选中的按钮
    self.selectedBtn = button;
    
    //4.跳转到相应的视图控制器. (通过selectIndex参数来设置选中了那个控制器)
    self.tb.selectedIndex = button.tag;
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
